import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_category.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/22.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///机考 选择分类
class ExamSelectClassPage extends StatefulWidget {
  //分类数据
  final List<CategoryBean> categoryList;

  const ExamSelectClassPage({Key key, this.categoryList}) : super(key: key);

  @override
  _ExamSelectClassPageState createState() => _ExamSelectClassPageState();
}

class _ExamSelectClassPageState extends State<ExamSelectClassPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("选择考试分类"),
      ),
      backgroundColor: Colors.white,

      ///填充布局
      body: ListView.builder(
        //一级分类数
        itemCount: widget.categoryList.length,
        itemBuilder: (BuildContext context, int index) {
          //封装成单独的类
          return CategoryLevelItemWidget(widget.categoryList[index], index);
        },
      ),
    );
  }
}

class CategoryLevelItemWidget extends StatefulWidget {
  final CategoryBean categoryBean;
  final int oneIndex;

  CategoryLevelItemWidget(this.categoryBean, this.oneIndex);

  @override
  State<StatefulWidget> createState() {
    return _CategoryLevelItemWidgetState();
  }
}

class _CategoryLevelItemWidgetState extends State<CategoryLevelItemWidget>
    with SingleTickerProviderStateMixin {
  bool _isExpand = false;

  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = new AnimationController(
        lowerBound: 0.0,
        upperBound: 0.25,
        vsync: this,
        duration: Duration(milliseconds: 400));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 14, right: 14, top: 10, bottom: 10),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          //一级分类
          buildHeaderInkWell(),
          //二级分类
          buildLevelWidget(),
        ],
      ),
    );
  }

  InkWell buildHeaderInkWell() {
    return InkWell(
      onTap: () {
        if (widget.categoryBean.children == null ||
            widget.categoryBean.children.length == 0) {
          ToastUtils.showToast("暂无二级分类数据");
          return;
        }

        if (_animationController.isCompleted) {
          _isExpand = false;
          _animationController.reverse();
        } else {
          _isExpand = true;
          _animationController.reset();
          _animationController.forward();
        }
        setState(() {});
      },
      child: Row(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(4)),
            child: Container(
              alignment: Alignment.center,
              width: 22,
              height: 22,
              color: Colors.blue,
              child: Text(
                "${widget.oneIndex}",
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
          SizedBox(
            width: 4,
          ),
          Expanded(
            child: Container(
              child: Text(
                "${widget.categoryBean.label}",
                style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: Color(0xff333333)),
              ),
            ),
          ),
          //旋转
          RotationTransition(
            turns: _animationController,
            child: Icon(Icons.arrow_right_rounded),
          ),
        ],
      ),
    );
  }

  buildLevelWidget() {
    //未展开时返回一个空的
    if (!_isExpand) {
      return Container();
    }
    //展开里返回二级分类
    return Container(
      margin: EdgeInsets.only(top: 12),
      child: ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        //ListView包裹子Widget
        shrinkWrap: true,
        //数量
        itemCount: widget.categoryBean.children.length,
        itemBuilder: (BuildContext context, int index) {
          CategoryItemBean itemBean = widget.categoryBean.children[index];
          return InkWell(
            onTap: () {
              //关闭当前页面 并回传数据
              Navigator.of(context).pop({
                "data": widget.categoryBean,
                "dataLevel": itemBean,
                "index": widget.oneIndex,
                "levelIndex": index,
              });
            },
            child: Container(
              padding: EdgeInsets.only(top: 6, bottom: 6, left: 22, right: 12),
              child: Row(
                children: [
                  Container(
                    margin: EdgeInsets.only(right: 4, top: 2),
                    padding:
                        EdgeInsets.only(top: 1, bottom: 1, left: 2, right: 2),
                    decoration: BoxDecoration(
                        color: Colors.green,
                        borderRadius: BorderRadius.all(Radius.circular(2))),
                    child: Text(
                      "ID$index",
                      style: TextStyle(
                        fontSize: 10,
                        color: Colors.white,
                      ),
                    ),
                  ),
                  Expanded(
                    child: Text("${itemBean.label}"),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
